<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="xdr-unpacker-objects.html" />
<link rel="prev" href="module-xdrlib.html" />
<link rel="parent" href="module-xdrlib.html" />
<link rel="next" href="xdr-unpacker-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>9.5.1 Packer Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="9.5 xdrlib  "
  href="module-xdrlib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="9.5 xdrlib  "
  href="module-xdrlib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="9.5.2 unpacker Objects"
  href="xdr-unpacker-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-xdrlib.html">9.5 xdrlib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-xdrlib.html">9.5 xdrlib  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="xdr-unpacker-objects.html">9.5.2 Unpacker Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0011510000000000000000"></a><a name="xdr-packer-objects"></a>
<br>
9.5.1 Packer Objects 
</h2>

<p>
<tt class="class">Packer</tt> instances have the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2099' xml:id='l2h-2099' class="method">get_buffer</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Returns the current pack buffer as a string.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2100' xml:id='l2h-2100' class="method">reset</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Resets the pack buffer to the empty string.
</dl>

<p>
In general, you can pack any of the most common XDR data types by
calling the appropriate <code>pack_<var>type</var>()</code> method.  Each method
takes a single argument, the value to pack.  The following simple data
type packing methods are supported: <tt class="method">pack_uint()</tt>,
<tt class="method">pack_int()</tt>, <tt class="method">pack_enum()</tt>, <tt class="method">pack_bool()</tt>,
<tt class="method">pack_uhyper()</tt>, and <tt class="method">pack_hyper()</tt>.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2101' xml:id='l2h-2101' class="method">pack_float</tt></b>(</nobr></td>
  <td><var>value</var>)</td></tr></table></dt>
<dd>
Packs the single-precision floating point number <var>value</var>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2102' xml:id='l2h-2102' class="method">pack_double</tt></b>(</nobr></td>
  <td><var>value</var>)</td></tr></table></dt>
<dd>
Packs the double-precision floating point number <var>value</var>.
</dl>

<p>
The following methods support packing strings, bytes, and opaque data:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2103' xml:id='l2h-2103' class="method">pack_fstring</tt></b>(</nobr></td>
  <td><var>n, s</var>)</td></tr></table></dt>
<dd>
Packs a fixed length string, <var>s</var>.  <var>n</var> is the length of the
string but it is <em>not</em> packed into the data buffer.  The string
is padded with null bytes if necessary to guaranteed 4 byte alignment.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2104' xml:id='l2h-2104' class="method">pack_fopaque</tt></b>(</nobr></td>
  <td><var>n, data</var>)</td></tr></table></dt>
<dd>
Packs a fixed length opaque data stream, similarly to
<tt class="method">pack_fstring()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2105' xml:id='l2h-2105' class="method">pack_string</tt></b>(</nobr></td>
  <td><var>s</var>)</td></tr></table></dt>
<dd>
Packs a variable length string, <var>s</var>.  The length of the string is
first packed as an unsigned integer, then the string data is packed
with <tt class="method">pack_fstring()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2106' xml:id='l2h-2106' class="method">pack_opaque</tt></b>(</nobr></td>
  <td><var>data</var>)</td></tr></table></dt>
<dd>
Packs a variable length opaque data string, similarly to
<tt class="method">pack_string()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2107' xml:id='l2h-2107' class="method">pack_bytes</tt></b>(</nobr></td>
  <td><var>bytes</var>)</td></tr></table></dt>
<dd>
Packs a variable length byte stream, similarly to <tt class="method">pack_string()</tt>.
</dl>

<p>
The following methods support packing arrays and lists:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2108' xml:id='l2h-2108' class="method">pack_list</tt></b>(</nobr></td>
  <td><var>list, pack_item</var>)</td></tr></table></dt>
<dd>
Packs a <var>list</var> of homogeneous items.  This method is useful for
lists with an indeterminate size; i.e. the size is not available until
the entire list has been walked.  For each item in the list, an
unsigned integer <code>1</code> is packed first, followed by the data value
from the list.  <var>pack_item</var> is the function that is called to pack
the individual item.  At the end of the list, an unsigned integer
<code>0</code> is packed.

<p>
For example, to pack a list of integers, the code might appear like
this:

<p>
<div class="verbatim"><pre>
import xdrlib
p = xdrlib.Packer()
p.pack_list([1, 2, 3], p.pack_int)
</pre></div>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2109' xml:id='l2h-2109' class="method">pack_farray</tt></b>(</nobr></td>
  <td><var>n, array, pack_item</var>)</td></tr></table></dt>
<dd>
Packs a fixed length list (<var>array</var>) of homogeneous items.  <var>n</var>
is the length of the list; it is <em>not</em> packed into the buffer,
but a <tt class="exception">ValueError</tt> exception is raised if
<code>len(<var>array</var>)</code> is not equal to <var>n</var>.  As above,
<var>pack_item</var> is the function used to pack each element.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-2110' xml:id='l2h-2110' class="method">pack_array</tt></b>(</nobr></td>
  <td><var>list, pack_item</var>)</td></tr></table></dt>
<dd>
Packs a variable length <var>list</var> of homogeneous items.  First, the
length of the list is packed as an unsigned integer, then each element
is packed as in <tt class="method">pack_farray()</tt> above.
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="9.5 xdrlib  "
  href="module-xdrlib.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="9.5 xdrlib  "
  href="module-xdrlib.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="9.5.2 unpacker Objects"
  href="xdr-unpacker-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-xdrlib.html">9.5 xdrlib  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-xdrlib.html">9.5 xdrlib  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="xdr-unpacker-objects.html">9.5.2 Unpacker Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
